//
//  HelpMeBagBankListVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/9.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "HelpMeBagBankListVC.h"
#import "HelpMeBagApplyNowVC.h"

#import "HelpMeBagBankListCell.h"

#import "HelpMeBagBankListModel.h"

static int from =0;

@interface HelpMeBagBankListVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView_bagBankList;

@property (nonatomic,strong) NSMutableArray *mutableArray_data;//存放所有数
@end

@implementation HelpMeBagBankListVC

- (NSMutableArray *)mutableArray_data
{
    if (!_mutableArray_data) {
        _mutableArray_data = [[NSMutableArray alloc] init];
    }
    return _mutableArray_data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"帮我贷";
    
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    //创建视图
    [self initWithHelpMeBagBankListVCSubviews];
    
    [self initWithRightButtonSubviews];

    //网络数据请求
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self DataSourceRequestHelpMeBagBankListView];
    
    //上下拉刷新
    [self setUprefresh];

}


#pragma mark --- right button
- (void) initWithRightButtonSubviews
{
    UIButton *button_rightChange = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_rightChange setTitle:@"返回首页"  forState:UIControlStateNormal];
    [button_rightChange setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    button_rightChange.frame = CGRectMake(15, 0, 60, 30);
    [button_rightChange.titleLabel setFont:[UIFont systemFontOfSize:24*PROPORTION_WIDTH]];
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button_rightChange];
    self.navigationItem.rightBarButtonItem = menuButton;
    
    [button_rightChange addTarget:self action:@selector(backToHomeViewAction)
                 forControlEvents:UIControlEventTouchUpInside];
    
}

//返回到首页
- (void) backToHomeViewAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark --- 创建视图 
- (void) initWithHelpMeBagBankListVCSubviews
{
    self.tableView_bagBankList = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:self.tableView_bagBankList];
    self.tableView_bagBankList.dataSource = self;
    self.tableView_bagBankList.delegate = self;
    self.tableView_bagBankList.showsVerticalScrollIndicator = NO;
    self.tableView_bagBankList.backgroundColor = COLOR_EVERYVIEWBG;
    self.tableView_bagBankList.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark --- UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mutableArray_data.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellHelpMeBagBankListID = @"cellHelpMeBagBankListID";
    HelpMeBagBankListCell *cell_bagList = [tableView cellForRowAtIndexPath:indexPath];
    if (cell_bagList==nil) {
        cell_bagList = [[HelpMeBagBankListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellHelpMeBagBankListID];
    }
    cell_bagList.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.mutableArray_data.count!=0) {
        cell_bagList.model_bagBankList = self.mutableArray_data[indexPath.row];
    }
    
    return cell_bagList;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.mutableArray_data.count!=0) {
        
        HelpMeBagBankListModel *model_bagBankList = self.mutableArray_data[indexPath.row];
        
        HelpMeBagApplyNowVC *applyNowView = [[HelpMeBagApplyNowVC alloc] init];
        
        applyNowView.dic_limitmin = (NSDictionary *)model_bagBankList.limitmin;
        applyNowView.model_baglist = model_bagBankList;
        
        [self.navigationController pushViewController:applyNowView animated:YES];
    }
   
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135*PROPORTION_WIDTH;
}


#pragma mark - 集成下拉刷洗 上拉加载
- (void)setUprefresh
{
    __weak __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView_bagBankList.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    //    [self.tableView.header beginRefreshing];
}

#pragma mark - 数据处理相关
#pragma mark 下拉刷新数据
- (void)loadNewData
{
    from=0;
    
    [self DataSourceRequestHelpMeBagBankListView];
    
    // 拿到当前的下拉刷新控件，结束刷新状态
    [self.tableView_bagBankList.header endRefreshing];
    
}

#pragma mark -- 网络数据请求
- (void) DataSourceRequestHelpMeBagBankListView
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    
    if (![UIView NetFromStatus:status]) {
        NSLog(@"没有网络");
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    else
    {
        
        NSString *strUrl = [NSString stringWithFormat:@"%@loan/bank/get",MRAPI];
        
        [[HttpRequest shardWebUtil] getNetworkRequestURLString:strUrl parameters:nil success:^(id obj) {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            NSLog(@"get%@",obj);
            
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0) {
                
                [self.mutableArray_data removeAllObjects];
                
                NSArray *array_data = obj[@"data"];
                if (array_data.count!=0) {
                    for (int i=0; i<array_data.count; i++) {
                        HelpMeBagBankListModel *model = [[HelpMeBagBankListModel alloc] init];
                        [model decodeDic:array_data[i]];
                        [self.mutableArray_data addObject:model];
                    }
                }
                else
                {
                    [BaseViewController showMessage:@"暂无数据"];
                }
             
                [self.tableView_bagBankList reloadData];
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
            
        } fail:^(NSError *error) {
            NSLog(@"get%@",error);
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [UIView showAlertMsg:@"网络请求超时"];

        }];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
